﻿using IBLL;
using IDAL;
using Microsoft.EntityFrameworkCore;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class ShopInfoBLL : IShopInfoBLL
    {
        private IShopInfoDAL _shopInfoDAL;
        private ICustomerInfoDAL _customerInfoDAL;
        private IProductInfoDAL _productInfoDAL;

        public ShopInfoBLL(
            IShopInfoDAL shopInfoDAL,
            ICustomerInfoDAL customerInfoDAL,
            IProductInfoDAL productInfoDAL
            )
        {
            _shopInfoDAL = shopInfoDAL;
            _customerInfoDAL = customerInfoDAL;
            _productInfoDAL = productInfoDAL;
        }

        #region 获取全部购物车表的方法 (GetShopInfoH)
        /// <summary>
        /// 获取购物车表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="departmentName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetShopInfoDTO> GetShopInfoH(int page, int limit,out int count)
        {
            var data = from s in _shopInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       join c in _customerInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       on s.CustomerId equals c.Id
                       into SCtemp
                       from sc in SCtemp.DefaultIfEmpty()
                       join p in _productInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       on s.ProductId equals p.Id
                       into SPTemp
                       from sp in SPTemp.DefaultIfEmpty()
                       select new GetShopInfoDTO
                       {
                           Id = s.Id,
                           ProductImage = sp.Image,
                           ProductPrice = sp.ProductPrice,
                           ProductName = sp.ProductName,
                           PurchaseNumber = s.PurchaseNumber,
                           CustomerName = sc.CustomerName,
                           CustomerAddress = sc.AddressId,
                           CreateTime = s.CreateTime
                       };
            count = data.Count();

            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;

        }
        #endregion

        #region 按顾客获取购物车表的方法 (GetShopInfos)
        /// <summary>
        /// 获取购物车表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="departmentName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetShopInfoDTO> GetShopInfo(int page, int limit, string customerId, out int count)
        {

            var data = from s in _shopInfoDAL.GetEntities().Where(it => it.CustomerId == customerId && it.IsDelete == false)
                       join c in _customerInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       on s.CustomerId equals c.Id
                       into SCtemp
                       from sc in SCtemp.DefaultIfEmpty()
                       join p in _productInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       on s.ProductId equals p.Id
                       into SPTemp
                       from sp in SPTemp.DefaultIfEmpty()
                       select new GetShopInfoDTO
                       {
                           Id = s.Id,
                           ProductImage = sp.Image,
                           ProductPrice = sp.ProductPrice,
                           ProductName = sp.ProductName,
                           PurchaseNumber = s.PurchaseNumber,
                           CustomerName = sc.CustomerName,
                           CustomerAddress = sc.AddressId,
                           CreateTime = s.CreateTime
                       };


            count = data.Count();

            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 创建购物车的方法 (CreateShopInfo)
        /// <summary>
        /// 创建购物车的方法
        /// </summary>
        /// <param name="shop">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateShopInfo(ShopInfo shop, out string msg)
        {
            if (string.IsNullOrWhiteSpace(shop.CustomerId))
            {
                msg = "顾客名称不能为空";
                return false;
            }
            if (shop.PurchaseNumber <= 0)
            {
                msg = "购买数量不能为0";
                return false;
            }
            CustomerInfo customer = _customerInfoDAL.GetEntities().FirstOrDefault(it => it.Id == shop.CustomerId);
            shop.Id = Guid.NewGuid().ToString();//用户id
            shop.CreateTime = DateTime.Now;//设置时间
            shop.CustomerId = customer.Id;
            bool IsSuccess = _shopInfoDAL.CreateEntity(shop);//调用方法
            msg = IsSuccess ? $"添加购物车成功" : "添加购物车失败";

            return IsSuccess;
        }
        #endregion

        #region 删除购物车的方法 (DeleteShopInfo)
        /// <summary>
        /// 删除购物车的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteShopInfo(string id)
        {
            ShopInfo shop = _shopInfoDAL.GetEntities().FirstOrDefault(it => it.Id == id);
            if (shop == null)
            {
                return false;
            }
            shop.IsDelete = true;
            shop.DeleteTime = DateTime.Now;
            _shopInfoDAL.UpdateEntity(shop);
            return true;
        }
        #endregion

        #region 批量删除购物车的方法 (DeleteListShopInfo)
        /// <summary>
        /// 批量删除购物车的方法
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteListShopInfo(List<string> ids)
        {
            List<ShopInfo> shopList = _shopInfoDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                ShopInfo shop = _shopInfoDAL.GetEntities().FirstOrDefault(it => it.Id == item);
                if (shopList == null)
                {
                    return false;
                }

                _shopInfoDAL.DeleteEntity(shop);
            }
            return true;
        }
        #endregion

        #region 修改购物车的方法 (UpdateShopInfo)
        /// <summary>
        /// 修改购物车的方法
        /// </summary>
        /// <param name="shop"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateShopInfo(ShopInfo shop, out string msg)
        {
            ShopInfo entity = _shopInfoDAL.GetEntities().FirstOrDefault(it => it.Id == shop.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }

            entity.PurchaseNumber = shop.PurchaseNumber;
            entity.OrderId = shop.OrderId;

            bool IsSuccess = _shopInfoDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion

        #region 根据ID获取数据返回赋值 (GetShopInfoById)
        /// <summary>
        /// 根据ID获取数据返回赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShopInfo GetShopInfoById(string id)
        {
            return _shopInfoDAL.GetEntities().FirstOrDefault(it => it.Id == id);
        }
        #endregion

        #region 获取数据库列表赋值下拉框 (GetSelectOption)
        /// <summary>
        /// 获取主管和父级购物车下拉框的方法
        /// </summary>
        /// <returns></returns>
        public object GetSelectOption()
        {
            var customerData = _customerInfoDAL.GetEntities().Where(it => it.Id != null).Select(d => new
            {
                value = d.Id,
                title = d.CustomerName,
            }).ToList();

            var productData = _productInfoDAL.GetEntities().Where(it => it.Id != null).Select(d => new
            {
                value = d.Id,
                title = d.ProductName,
            }).ToList();

            return new
            {
                customerData = customerData,
                productData = productData
            };
        }
        #endregion
    }
}
